package com.kye.express.business.waybill_list.subfragment;

import com.kye.base.utils.DateUtils;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.PageInfo;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.logistics.LogisticsListResultBean;
import com.kye.model.bean.logistics.request.RequestLogisticsList;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.LogisticsDataSource;

import java.util.Date;

import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public interface WaybillListSignedContract {

    interface WaybillListSignedView extends IView, LoadingView, ErrorView {

        void onUser(User user);

        void refreshInitSuccess(OpenApiResult<LogisticsListResultBean> result, LogisticsListResultBean logisticsListResultBean);

        void refreshInitFailed(String msg);

        void refreshInitFailed(Throwable e);

        void refreshSuccess(LogisticsListResultBean logisticsListResultBean);

        void refreshFailed(String msg);

        void refreshFailed(Throwable e);

        void loadMoreSuccess(LogisticsListResultBean logisticsListResultBean);

        void loadMoreFailed(String msg);

        void loadMoreFailed(Throwable e);
    }

    class WaybillListSignedPresenter extends BasePresenter<WaybillListSignedView> {

        private LogisticsDataSource logisticsDataSource;
        private Observable<User> userObservable;
        private User userInfo;
        private PageInfo pageInfo;

        public WaybillListSignedPresenter() {
            logisticsDataSource = new LogisticsDataSource();
        }

        @Override
        public void bind(WaybillListSignedView view) {
            super.bind(view);
            pageInfo = new PageInfo();
            userObservable = UserSession.getInstance().getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }


        public void refreshInit(final Date from, final Date to) {
            Single.just(new RequestLogisticsList())
                    .doOnSuccess(new Consumer<RequestLogisticsList>() {
                        @Override
                        public void accept(RequestLogisticsList requestLogisticsList) throws Exception {
                            requestLogisticsList.setPhone(userInfo.getPhone());
                            requestLogisticsList.setOrderMan(userInfo.getUserName());
                            requestLogisticsList.setStartTime(DateUtils.getDateStringByDateYearMonthDay(from));
                            requestLogisticsList.setEndTime(DateUtils.getDateStringByDateYearMonthDay(to));
                            requestLogisticsList.setPageIndex(PageInfo.INDEX_DEFAULT);
                            requestLogisticsList.setPageSize(PageInfo.SIZE_DEFAULT);
                            requestLogisticsList.setQueryState("1");
                        }
                    })
                    .flatMap(new Function<RequestLogisticsList, SingleSource<OpenApiResult<LogisticsListResultBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<LogisticsListResultBean>> apply(RequestLogisticsList requestLogisticsList) throws Exception {
                            return logisticsDataSource
                                    .getLogisticsList(requestLogisticsList)
                                    .compose(getView().<OpenApiResult<LogisticsListResultBean>>bindToLifecycleDestroy());
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<LogisticsListResultBean>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<LogisticsListResultBean> result, LogisticsListResultBean logisticsListResultBean) {
                            pageInfo.resetPage();
                            getView().refreshInitSuccess(result, logisticsListResultBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<LogisticsListResultBean> result, int code, String msg) {
                            getView().refreshInitFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().refreshInitFailed(e);
                        }

                        @Override
                        public void onLoaded() {

                        }
                    });
        }

        public void refresh(final Date from, final Date to) {
            Single.just(new RequestLogisticsList())
                    .doOnSuccess(new Consumer<RequestLogisticsList>() {
                        @Override
                        public void accept(RequestLogisticsList requestLogisticsList) throws Exception {
                            requestLogisticsList.setPhone(userInfo.getPhone());
                            requestLogisticsList.setOrderMan(userInfo.getUserName());
                            requestLogisticsList.setStartTime(DateUtils.getDateStringByDateYearMonthDay(from));
                            requestLogisticsList.setEndTime(DateUtils.getDateStringByDateYearMonthDay(to));
                            requestLogisticsList.setPageIndex(PageInfo.INDEX_DEFAULT);
                            requestLogisticsList.setPageSize(PageInfo.SIZE_DEFAULT);
                            requestLogisticsList.setQueryState("1");
                        }
                    })
                    .flatMap(new Function<RequestLogisticsList, SingleSource<OpenApiResult<LogisticsListResultBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<LogisticsListResultBean>> apply(RequestLogisticsList requestLogisticsList) throws Exception {
                            return logisticsDataSource
                                    .getLogisticsList(requestLogisticsList)
                                    .compose(getView().<OpenApiResult<LogisticsListResultBean>>bindToLifecycleDestroy());
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<LogisticsListResultBean>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<LogisticsListResultBean> result, LogisticsListResultBean logisticsListResultBean) {
                            pageInfo.resetPage();
                            getView().refreshSuccess(logisticsListResultBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<LogisticsListResultBean> result, int code, String msg) {
                            getView().refreshFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().refreshFailed(e);
                        }

                        @Override
                        public void onLoaded() {

                        }
                    });
        }

        public void loadMore(final Date from, final Date to) {
            Single.just(new RequestLogisticsList())
                    .doOnSuccess(new Consumer<RequestLogisticsList>() {
                        @Override
                        public void accept(RequestLogisticsList requestLogisticsList) throws Exception {
                            requestLogisticsList.setPhone(userInfo.getPhone());
                            requestLogisticsList.setOrderMan(userInfo.getUserName());
                            requestLogisticsList.setStartTime(DateUtils.getDateStringByDateYearMonthDay(from));
                            requestLogisticsList.setEndTime(DateUtils.getDateStringByDateYearMonthDay(to));
                            requestLogisticsList.setPageIndex(pageInfo.getPageIndex() + 1);
                            requestLogisticsList.setPageSize(PageInfo.SIZE_DEFAULT);
                            requestLogisticsList.setQueryState("1");
                        }
                    })
                    .flatMap(new Function<RequestLogisticsList, SingleSource<OpenApiResult<LogisticsListResultBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<LogisticsListResultBean>> apply(RequestLogisticsList requestLogisticsList) throws Exception {
                            return logisticsDataSource
                                    .getLogisticsList(requestLogisticsList)
                                    .compose(getView().<OpenApiResult<LogisticsListResultBean>>bindToLifecycleDestroy());
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<LogisticsListResultBean>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<LogisticsListResultBean> result, LogisticsListResultBean logisticsListResultBean) {
                            pageInfo.resetPage();
                            getView().loadMoreSuccess(logisticsListResultBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<LogisticsListResultBean> result, int code, String msg) {
                            getView().loadMoreFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().loadMoreFailed(e);
                        }

                        @Override
                        public void onLoaded() {

                        }
                    });
        }

        public void getUserInfo() {
            userObservable
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            userInfo = user;
                            getView().onUser(user);
                        }
                    });
        }

        public Observable<User> getUserObservable() {
            return userObservable;
        }
    }
}
